#include "Panels.h"


#include <wx/stattext.h>
#include <Communicate.h>
#include "wxSFMLCanvas.hpp"
#include "data\slide_show.hpp"
#include <resource_cache\resource_cache.hpp>
#include "../string_conv.h"
#include "../shortcuts.hpp"

void MyCanvas::OnMouseMove(wxMouseEvent& Event)
{
	int x = Event.GetX();
	int y = Event.GetY();

	wxString str;
	str.Append( "X: " );
	str.Append( wxString::Format(wxT("%d"), x) );
	str.Append( "  Y: " );
	str.Append( wxString::Format(wxT("%d"), y) );

	RightPanel *rpcomm = (RightPanel *) m_parent->GetParent();
	Communicate *comm = (Communicate *) rpcomm->GetParent();
	comm->m_rp->m_text->SetLabel(str);
}
void MyCanvas::on_mouse_click(wxMouseEvent& Event)
{
	int x = Event.GetX();
	int y = Event.GetY();

	RightPanel *rpcomm = (RightPanel *) m_parent->GetParent();
	Communicate *comm = (Communicate *) rpcomm->GetParent();

	if( g_slides.active_body_pos && active_body())
	{
		CBODY.position.x = x;
		CBODY.position.y = y;
		
		comm->m_lp->m_slide_data.m_body_ui.m_positionX.set_text(stringify(x));
		comm->m_lp->m_slide_data.m_body_ui.m_positionY.set_text(stringify(y));
	}

	if( g_slides.active_circle_pos && active_shape())
	{
		CSHAPE.circle_offset.x = x-CBODY.position.x;
		CSHAPE.circle_offset.y = y-CBODY.position.y;
		
		comm->m_lp->m_slide_data.m_shape_ui.m_circle_offset_x.set_text(stringify(x-CBODY.position.x));
		comm->m_lp->m_slide_data.m_shape_ui.m_circle_offset_y.set_text(stringify(y-CBODY.position.y));
	}

	int cslide = g_slides.current_slide; int cbody = CSLIDE.current_body;
	int ckine  = CBODY.current_kinematic; bool ac_k = true;
	if( cslide < 0 || cslide >= (int)g_slides.slides.size() ||
		cbody < 0 || cbody >= (int)g_slides.slides[cslide].bodys.size() ||
		ckine < 0 || ckine >= (int)CBODY.kinematic_points.size())
		ac_k = false;
	if( g_slides.active_kine_pos && ac_k)
	{
		CBODY.kinematic_points[ckine].point.x = x;
		CBODY.kinematic_points[ckine].point.y = y;
		
		comm->m_lp->m_slide_data.m_body_ui.m_kineX.set_text(stringify(x));
		comm->m_lp->m_slide_data.m_body_ui.m_kineY.set_text(stringify(y));
	}
	
	if( g_slides.active_vertex_pos && active_vertex())
	{
		CSHAPE.polygon_points[CSHAPE.current_polygon].x = x-CBODY.position.x;
		CSHAPE.polygon_points[CSHAPE.current_polygon].y = y-CBODY.position.y;
		
		comm->m_lp->m_slide_data.m_shape_ui.m_vertex_x.set_text(stringify(x-CBODY.position.x));
		comm->m_lp->m_slide_data.m_shape_ui.m_vertex_y.set_text(stringify(y-CBODY.position.y));
	}
}

void MyCanvas::OnUpdate()
{
    //if (GetInput().IsMouseButtonDown(sf::Mouse::Left))  mySprite.Rotate( GetFrameTime() * 50);
    //if (GetInput().IsMouseButtonDown(sf::Mouse::Right)) mySprite.Rotate(-GetFrameTime() * 50);

    Clear(sf::Color(0, 128, 128));

	int cslide = g_slides.current_slide; 
	int cbody = -1; int cshape = -1;
	if( cslide >= 0 && cslide < g_slides.slides.size() )
		cbody= g_slides.slides[cslide].current_body;
	if( cbody >= 0 && cbody < g_slides.slides[cslide].bodys.size())
		cshape= g_slides.slides[cslide].bodys[cbody].current_shape;

	if( cslide >= 0 )
		Draw(g_slides.slides[cslide].background);

	if( cslide >= 0 )
	{
		for( unsigned int i=0; i < CSLIDE.bodys.size(); ++i )
		{
			CSLIDE.bodys[i].animation_sprite.update(clock.GetElapsedTime());
			clock.Reset();
			CSLIDE.bodys[i].animation_sprite.draw(this,CSLIDE.bodys[i].position.x, CSLIDE.bodys[i].position.y,CSLIDE.bodys[i].angle);
		}
	}
	if( active_body() )
	{
		if( CBODY.body_type == body_kinematic )
		{
			float x1 = 0;
			float y1 = 0;
			for( int k=0; k < CBODY.kinematic_points.size(); ++k )
			{
				if( k==0 )
				{
					x1 = CBODY.kinematic_points[k].point.x;
					y1 = CBODY.kinematic_points[k].point.y;
				}
				else
				{
					sf::Shape line   = sf::Shape::Line(x1, y1, CBODY.kinematic_points[k].point.x, CBODY.kinematic_points[k].point.y,
						1, sf::Color(0, 255, 0, 210),  0,   sf::Color(0,255,0) );
					x1 = CBODY.kinematic_points[k].point.x;
					y1 = CBODY.kinematic_points[k].point.y;
					Draw(line);
				}
			}
		}
	}
	if( active_body() )
	{
		for( unsigned int i=0; i < CBODY.shapes.size(); ++i )
		{
			int fill = 0;
			int line = 180;
			if( i == CBODY.current_shape )
			{
				fill = 40;
				line = 255;
			}
			if( CBODY.shapes[i].shape_type == shape_circle )
			{
				sf::Shape Circle = sf::Shape::Circle(CBODY.shapes[i].circle_offset.x+CBODY.position.x, CBODY.shapes[i].circle_offset.y+CBODY.position.y, 
					CBODY.shapes[i].circle_radius, sf::Color(255,0,0,fill), 1, sf::Color(255,0,0,line));
				Draw(Circle);
			}
			else if( CBODY.shapes[i].shape_type == shape_polygon )
			{
				sf::Shape polygon;
				polygon.SetOutlineWidth(1);
				for( unsigned int p=0; p<CBODY.shapes[i].polygon_points.size(); ++p )
				{
					polygon.AddPoint(CBODY.shapes[i].polygon_points[p].x+CBODY.position.x, CBODY.shapes[i].polygon_points[p].y+CBODY.position.y,  
						sf::Color(255, 0, 0,fill),     sf::Color(255,0,0,line));
				}
				Draw(polygon);
			}
		}
	}
	

    //Draw(mySprite);
}